﻿Imports System.Drawing.Drawing2D



Public Class xTabControl
    Inherits TabControl
    Dim itemWidth As Integer = 200
    Dim xfont As New Font("Arial", 8)
    Dim lb As New Label
    Dim _mouseInBox As Boolean
    Dim _boxRect As Rectangle
    Dim ItemBounds As Rectangle
    Dim TextBrush As New SolidBrush(Color.Empty)
    Dim TabBrush As New SolidBrush(Color.Gray)
    Dim xStart As Integer
    Dim xEnd As Integer
    Dim xTop As Integer
    Dim xbottom As Integer
    Dim BorderColor As Color
    Dim sf As New StringFormat
    Dim BoxPen As Pen
    Dim _mouseInTab As Integer = 0

    Dim rectList As New List(Of Rectangle)

    Public Sub New()
        MyBase.New()
        SetStyle(ControlStyles.AllPaintingInWmPaint Or _
        ControlStyles.ResizeRedraw Or _
        ControlStyles.UserPaint Or _
        ControlStyles.DoubleBuffer, True)

        Dim s As New Size(itemWidth, 25)
        Me.ItemSize = s
        SizeMode = TabSizeMode.Normal
        sf.LineAlignment = StringAlignment.Center
        sf.Alignment = StringAlignment.Center
        _mouseInTab = -1
        SetupShapes()
    End Sub
    Private Sub SetupShapes()
        For TabItemIndex As Integer = 0 To Me.TabCount - 1
            ItemBounds = Me.GetTabRect(TabItemIndex)
            rectList.Add(ItemBounds)
        Next
    End Sub

    Private Function thisTab(ByRef p As Point) As Integer
        Dim result As Integer = -1
        For TabItemIndex As Integer = 0 To Me.TabCount - 1
            ItemBounds = Me.GetTabRect(TabItemIndex)
            If ItemBounds.Contains(p) Then
                result = TabItemIndex
                Exit For
            End If
        Next
        Return result
    End Function
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)

        e.Graphics.Clear(BackColor)
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        'Draw the tab items
        For TabItemIndex As Integer = 0 To Me.TabCount - 1
            ItemBounds = Me.GetTabRect(TabItemIndex)
            xStart = ItemBounds.Left + 4
            xEnd = ItemBounds.Right
            xTop = ItemBounds.Top - 0
            xbottom = ItemBounds.Bottom + 2

            Dim ptx() As Point = {New Point(xStart, xbottom),
                                  New Point(xStart, xbottom - 4),
                                  New Point(xStart + 8, xTop),
                                  New Point(xEnd, xTop),
                                  New Point(xEnd, xbottom)}


            Dim _box() As Point = {New Point(xEnd - 12, xbottom - 3),
                                  New Point(xEnd - 12, xTop + 3),
                                  New Point(xEnd - 2, xTop + 3),
                                  New Point(xEnd - 2, xbottom - 3),
                                  New Point(xEnd - 12, xbottom - 3),
                                  New Point(xEnd - 2, xTop + 3),
                                  New Point(xEnd - 2, xbottom - 3),
                                  New Point(xEnd - 12, xTop + 3)}

            If TabItemIndex = SelectedIndex Then
                TabBrush.Color = Color.FromArgb(255, 255, 215)
                BorderColor = Color.FromArgb(75, 75, 75)
                TextBrush.Color = Color.Black
            Else
                If TabItemIndex = _mouseInTab Then
                    TabBrush.Color = Color.FromArgb(120, 120, 175)
                    BorderColor = Color.FromArgb(255, 255, 25)
                    TextBrush.Color = Color.LightYellow
                Else
                    TabBrush.Color = Color.FromArgb(95, 95, 150)
                    BorderColor = Color.FromArgb(255, 255, 25)
                    TextBrush.Color = Color.WhiteSmoke
                End If

            End If
            Dim xpath As New GraphicsPath()
            xpath.AddLines(ptx)

            Dim BorderPen As New Pen(BorderColor, 1)
            e.Graphics.DrawPath(BorderPen, xpath)
            e.Graphics.FillPolygon(TabBrush, ptx)

            e.Graphics.DrawString( _
            Me.TabPages(TabItemIndex).Text, _
            xfont, TextBrush, _
            RectangleF.op_Implicit(Me.GetTabRect(TabItemIndex)), sf)

            If TabItemIndex = SelectedIndex Then
                If _mouseInBox Then
                    BoxPen = New Pen(Color.LightGray, 1)
                Else
                    BoxPen = New Pen(Color.Gray, 1)
                End If
            Else
                BoxPen = New Pen(Color.DarkGray, 1)

            End If


            Dim xbox As New GraphicsPath()

            xbox.AddLines(_box)
            e.Graphics.DrawPath(BoxPen, xbox)
        Next

    End Sub

    Private Sub xtabcontrolzz_ControlAdded(sender As Object, e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
        rectList.Clear()
        For TabItemIndex As Integer = 0 To Me.TabCount - 1
            ItemBounds = Me.GetTabRect(TabItemIndex)
            rectList.Add(ItemBounds)
        Next
    End Sub


    Private Sub xtabcontrolzz_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        If isInBox(e.Location) Then
            CloseTab()
        End If
    End Sub

    Private Sub xtabcontrolzz_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If Not isInBox(e.Location) Then
            _mouseInTab = thisTab(e.Location)
        End If

    End Sub
    Private Function isInBox(ByRef px As Point) As Boolean
        Dim ItemBounds As Rectangle
        Dim p As Point = px
        If SelectedIndex > -1 Then
            ItemBounds = Me.GetTabRect(SelectedIndex)
            Dim xStart As Integer = ItemBounds.Left + 4
            Dim xEnd As Integer = ItemBounds.Right
            Dim xTop As Integer = ItemBounds.Top - 0
            Dim xbottom As Integer = ItemBounds.Bottom + 2
            _boxRect = New Rectangle(xEnd - 12, xTop + 3, 10, 10)
            If _boxRect.Contains(p) Then
                _mouseInBox = True
            Else
                _mouseInBox = False
            End If
        End If

        Invalidate(ItemBounds)
        Return _mouseInBox
    End Function

    Private Sub xtabcontrolz_DoubleClick(sender As Object, e As System.EventArgs) Handles Me.DoubleClick
        CloseTab()
    End Sub

    Private Sub xTabControlz_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
        _mouseInTab = -1
        Refresh()
    End Sub

    Private Sub CloseTab()
        If MessageBox.Show("Remove the current tab?", "Remove tab", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Dim t As xTab = CType(SelectedTab, xTab)
            TabPages.Remove(SelectedTab)
            t.Dispose()
            t = Nothing
        End If
    End Sub

    Private Sub xtabcontrolz_ControlRemoved(sender As Object, e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlRemoved
        rectList.Clear()
        For TabItemIndex As Integer = 0 To Me.TabCount - 1
            ItemBounds = Me.GetTabRect(TabItemIndex)
            rectList.Add(ItemBounds)
        Next
    End Sub
End Class

